import { Component } from '@angular/core';
import {AlertController, NavController, NavParams, ToastController} from "ionic-angular";

import { LogInPage } from './log-in';
import { User } from './User';
import {LoginNewService} from "./login-new.service";

@Component({
  selector: '',
  templateUrl: 'enroll.html'
})

export class EnrollPage {
  passSameFlag : boolean;

  username : string;

  password1 : string;
  password2 : string;

  user:User;
   res:string;
  cityData : any[];

  constructor(public navCtrl:NavController,
              public params:NavParams,
              public alertCtrl: AlertController,
              public toastCtrl: ToastController,
              public loginNewService : LoginNewService,) {

    this.passSameFlag = false;
  }



  reset(){
    this.username = "";

    this.password1 = "";
    this.password2 = "";
  }

  doSubmit(){
    if(this.username==null||this.password1==null||this.password2==null){
      let alert = this.alertCtrl.create({
        subTitle: '信息不完整！',
        buttons: ['确定']
      });
      alert.present();
    // }else if (!/^[a-zA-Z0-9]{6,16}$/.test(this.password1)){
    //   let alert = this.alertCtrl.create({
    //     subTitle: '密码长度限制在6位到16位之间，请重新输入',
    //     buttons: ['确定']
    //   });
    //   alert.present();
    //   this.password1 = "";
    //   this.password2 = "";
    } else if (!this.passSameFlag){
      let alert = this.alertCtrl.create({
        subTitle: '两次密码不一致，请重新输入',
        buttons: ['确定']
      });
      alert.present();
      this.password1 = "";
      this.password2 = "";
    } else if(this.username!=null && this.password1!=null && this.password2!=null && this.passSameFlag == true ){
      this.user=new User(this.username,this.password1);
      this.loginNewService.signup(this.user).then(()=>{
        //this.loginService.loginMap.set(this.identification, new Guardian(this.identification, this.name, this.password1));
        console.log("SignupService");
        this.presentToast();
        this.navCtrl.push(LogInPage,{
          username : this.username,
          password : this.password1,
          autoLoginFlag : true,
        });
      });
    }
    else{
      this.showFailedAlert();
    }
  }

  showSuccessAlert() {
    let alert = this.alertCtrl.create({
      subTitle: '注册成功！',
      buttons: ['确定']
    });
    alert.present();
  }
  showFailedAlert(){
    let alert = this.alertCtrl.create({
      subTitle: '注册失败！',
      buttons: ['确定']
    });
    alert.present();
  }
  reInput($event){
    if(this.password1 != this.password2){
      this.passSameFlag = false;
    }
    else{
      this.passSameFlag = true;
    }
  }
  isReInputCorrect(){
    if(this.password1 != this.password2){
      this.passSameFlag = false;
    }
    else{
      this.passSameFlag = true;
    }
  }
  presentToast() {
    let toast = this.toastCtrl.create({
      message: '注册成功！',
      duration: 1500,
      position: 'top'
    });
    toast.present();
  }
  isExist(){
    console.log("istrue");
    this.loginNewService.isExist(this.username).then(response=>{
        this.res=response;
        console.log(this.res);
        let body=this.res.toString();
        console.log("this.body "+this.res);
       if(body=='false'){
         console.log("用户不存在");
       }
       else {
          console.log("body已存在");
          let alert = this.alertCtrl.create({
            subTitle: '账户已存在',
            buttons: ['确定']
          });
          alert.present();
          this.username='';
          this.password1='';
          this.password2='';
        }
    }

    )
  }

}
